<?php

namespace app\common\model;

use think\Request;
use think\Exception;
use think\Db;
/**
 * 用户返佣金
 * Class User
 * @package app\common\model
 */
class MemberTips 
{

   /**
   * 计算每一笔订单的佣金
   * @param string $userId    用户id
   * @param $post['change']   变动金额
   * @param $post['details']  变动说明
   */
   	public static function getOrderTips($orderId = '')
   	{
   		//$eventOrder = EventOrder::getEventOrder(156);
   		$eventOrder = EventOrder::getEventOrder($orderId);
   		//缺少过滤条件。什么状态的订单才能返佣金,返佣金以后修改订单状态为已经反佣金
   		//print_r($eventOrder);die();
   		$event = Event::getEvent($eventOrder['event_id']);
   		//print_r($event);die();
   		$member = Member::getMemberInfo($eventOrder['uid']); //下单用户
   		$rank = MemberRank::getRank($member['type']);
   		//print_r($member);die();
   		$upMember = Member::getMemberInfo($member['from_uid'],false); //下单用户的上级
   		$upRank = '';
   		if ($upMember) {
   			$upRank = MemberRank::getRank($upMember['type'], false);
   		}
   		$rateArray = self::caclTips($rank, $upRank); // 计算返佣比例
   		$adult_number = 3; //成人数量
   		$child_number = 2; //儿童数量
   		//计算本级成人下单佣金
   		$adultTip = $rateArray['rate'] * $adult_number * $event['adult_tip']; 
   		//计算本级儿童下单佣金
   		$childTip = $rateArray['rate'] * $child_number * $event['child_tip'];
   		$tip = $adultTip+$childTip;
   		$post = [
   			'change' => $tip,
   			'details' => '订单反佣金',
   			'link_id' => 'order_id'.$eventOrder['id']
   		]
   		MemberInfo::incrMoney($eventOrder['uid'],$post); //发放本机佣金
   		if ($rateArray['upRate']) {
   			//计算上级成人下单佣金
	   		$upAdultTip = $rateArray['upRate'] * $adult_number * $event['adult_tip']; 
	   		//计算上级儿童下单佣金
	   		$upChildTip = $rateArray['upRate'] * $child_number * $event['child_tip'];
   			$upTip = $upAdultTip + $upChildTip;
   			$post = [
	   			'change' => $tip,
	   			'details' => '订单反佣金',
	   			'link_id' => 'order_id'.$eventOrder['id']
   			]
   			MemberInfo::incrMoney($eventOrder['uid'],$post); //发放本机佣金
   		}
   		return true;	
   	}

   	public static function getTips

   	public static function caclTips($rank = '', $upRank = '')
   	{
   		$keguan = '0.1'; 	//客官
   		$wanjia = '0.3';	//玩家
   		$wanjia2 = '0.1';
   		$zhanggui1 = '0.7';   //掌柜 两种返佣可能 一种 70% 另一种90%
   		$zhanggui2 = '0.9';
   		$zhanggui3 = '0.1';
   		$rate = '';  	//本级返佣比例
   		$upRate = ''; 	//上级返佣比例

   		if (!$upRank) {
   			switch ($rank['id']) {
   				case MemberRank::KEGUAN:
   					$rate = $keguan;
   					break;
   				case MemberRank::WANJIA:
   					$rate = $wanjia;
   					break;
   				case MemberRank::ZHANGGUI:
   					$rate = $zhanggui2;
   					break;	
   				default:
   					break;
   			}
   			$return = ['rate' => $rate, 'upRate' => $upRate];
   			//print_r($return);
   			return $return;
   		}
   		if($rank['id'] == MemberRank::KEGUAN && $upRank['id'] == MemberRank::KEGUAN) {
   			$rate = $keguan;
   			$upRate = $keguan;
   		}
   		if($rank['id'] == MemberRank::KEGUAN && $upRank['id'] == MemberRank::WANJIA) {
   			$rate = $keguan;
   			$upRate = $wanjia;
   		}
   		if($rank['id'] == MemberRank::KEGUAN && $upRank['id'] == MemberRank::ZHANGGUI) {
   			$rate = $keguan;
   			$upRate = $zhanggui2;
   		}
   		if($rank['id'] == MemberRank::WANJIA && $upRank['id'] == MemberRank::KEGUAN) {
   			$rate = $wanjia;
   			$upRate = $keguan;
   		}
   		if($rank['id'] == MemberRank::WANJIA && $upRank['id'] == MemberRank::WANJIA) {
   			$rate = $wanjia;
   			$upRate = $wanjia;
   		}
   		if($rank['id'] == MemberRank::WANJIA && $upRank['id'] == MemberRank::ZHANGGUI) {
   			$rate = $wanjia;
   			$upRate = $zhanggui1;
   		}
   		if($rank['id'] == MemberRank::ZHANGGUI && $upRank['id'] == MemberRank::KEGUAN) {
   			$rate = $zhanggui2;
   			$upRate = $keguan;
   		}
   		if($rank['id'] == MemberRank::ZHANGGUI && $upRank['id'] == MemberRank::WANJIA) {
   			$rate = $zhanggui2;
   			$upRate = $wanjia2;
   		}
   		if($rank['id'] == MemberRank::ZHANGGUI && $upRank['id'] == MemberRank::ZHANGGUI) {
   			$rate = $zhanggui2;
   			$upRate = $zhanggui3;
   		}
   		$return = ['rate' => $rate, 'upRate' => $upRate];
   		//print_r($return);die();
   		return $return;
   	}
}
